import _ from 'lodash'
import moment from 'moment'

/**
 * 返回年化系数 = 实际期限/计息基准天数
 * @param object
 */
export function calcAnnualized(daycounter, term, valueDate) {
    let year = valueDate.split('-')[0];
    var days = 365;
    if (daycounter == 'Actual/365') {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            days = 366;
        } else {
            days = 365;
        }
    } else {
        days = Boolean(daycounter)? daycounter.replace(/[^\d]/g, ''): 365;
    }
    return _.divide(term, days);
}

/**
 *  判断是否为空  null empty  blank undefind NaN
 * @param object
 */
export function isVoid(param) {
    if ((_.isNull(param) || _.isEmpty(param) || _.isUndefined(param) || param == '' || "undefined" == param)
        && !Number.isFinite(param)
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 *  判断是否为空  null empty  blank undefind
 *  多个参数
 * @param object
 */
export function isVoidArg() {
    var n = arguments.length;
    var t = true;
    for (var i = 0; i < arguments.length; i++) {
        if (isNotVoid(arguments[i])) {
            t = false;
            break
        }
    }
    return t;
}

/**
 *  判断是否为空  null empty  blank undefind
 * @param object
 */
export function isNotVoid(param) {
    return !isVoid(param);
}

/**
 *  判断是否为空  null empty  blank undefind
 *  多个参数
 * @param object
 */
export function isNotVoidArg() {
    var n = arguments.length;
    var t = true;
    for (var i = 0; i < arguments.length; i++) {
        if (isVoid(arguments[i])) {
            t = false;
            break
        }
    }
    return t;
}


/**
 *  判断ab  a b Annualized  是否相等  年化系数
 * @param object
 */
export function isEquativesAnnualizedABC(mnotional, mactualPecentage, mannualized, mpaymentAmount,) {
    if (isEqualNum(mactualPecentage, div(mpaymentAmount, mul(mannualized, mnotional, 10), 10)) ||
        isEqualNum(mnotional, div(mpaymentAmount, mul(mannualized, mactualPecentage, 10), 10)) ||
        isEqualNum(mnotional, div(div(mpaymentAmount, mactualPecentage, 10), mannualized,10)) ||
        isEqualNum(mpaymentAmount, mul(mnotional, mul(mannualized, mactualPecentage, 10),10)) ||
        isEqualNum(mpaymentAmount, mul(mactualPecentage, mul(mannualized, mnotional, 10),10))) {
        return true;
    } else {
        return false;
    }
}

/**
 *  判断ab  a b  是否相等
 * @param object
 */
export function isEquativesABC(ma, mb, mab) {

    if (_.isEqual(round(mb, 10), round(_.divide(mab, ma), 10)) ||
        _.isEqual(round(ma, 10), round(_.divide(mab, mb), 10)) ||
        _.isEqual(round(mab, 10), round(_.multiply(mb, ma), 10))) {
        return true;
    } else {
        return false;
    }

}

/**
 *  乘 多个参数
 * */
export function mulArg(...nums) {
    let num = 1;
    nums.forEach((item) => {
        let mitem = _.toNumber(item);
        num *= mitem
    })
    return _.round(num, 2);
}

/**
 * 相等
 * */
export function isEqualNum(m, n) {
    if (round(m,10) == round(n,10)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 乘
 * */
export function mul(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.multiply(m, n), p);
}

/**
 * 除
 * */
export function div(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.divide(m, n), p);
}

/**
 * 加
 * */
export function add(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.add(m, n), p);
}

/**
 * 减
 * */
export function sub(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.subtract(m, n), p);
}

/**
 * 四舍五入 精度
 * */
export function round(n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.toNumber(n), p);
}
export function zhDigitToArabic(digit) {
    const zh = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const unit = ['千', '百', '十'];
    const quot = ['万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祗', '那由他', '不可思议', '无量', '大数'];
    let result = 0, quotFlag;

    for (let i = digit.length - 1; i >= 0; i--) {
        if (zh.indexOf(digit[i]) > -1) { // 数字
            if (quotFlag) {
                result += quotFlag * getNumber(digit[i]);
            } else {
                result += getNumber(digit[i]);
            }
        } else if (unit.indexOf(digit[i]) > -1) { // 十分位
            if (quotFlag) {
                result += quotFlag * getUnit(digit[i]) * getNumber(digit[i - 1]);
            } else {
                result += getUnit(digit[i]) * getNumber(digit[i - 1]);
            }
            --i;
        } else if (quot.indexOf(digit[i]) > -1) { // 万分位
            if (unit.indexOf(digit[i - 1]) > -1) {
                if (getNumber(digit[i - 1])) {
                    result += getQuot(digit[i]) * getNumber(digit[i - 1]);
                } else {
                    result += getQuot(digit[i]) * getUnit(digit[i - 1]) * getNumber(digit[i - 2]);
                    quotFlag = getQuot(digit[i]);
                    --i;
                }
            } else {
                result += getQuot(digit[i]) * getNumber(digit[i - 1]);
                quotFlag = getQuot(digit[i]);
            }
            --i;
        }
    }

    return result;

    // 返回中文大写数字对应的阿拉伯数字
    function getNumber(num) {
        for (let i = 0; i < zh.length; i++) {
            if (zh[i] == num) {
                return i;
            }
        }
    }

    // 取单位
    function getUnit(num) {
        for (let i = unit.length; i > 0; i--) {
            if (num == unit[i - 1]) {
                return Math.pow(10, 4 - i);
            }
        }
    }

    // 取分段
    function getQuot(q) {
        for (var i = 0; i < quot.length; i++) {
            if (q == quot[i]) {
                return Math.pow(10, (i + 1) * 4);
            }
        }
    }
}

/**
 * 返回年化系数 =（到期日-开始日）/计息基准天数
 * @param object
 */
export function calcAnnualizedByTerminationPaymentDate(data) {
    let {effectiveDate,daycounter,terminationDate, term} = data
    let year = effectiveDate.split('-')[0];
    var days = 365;
    let a = moment(terminationDate)
    let b = moment(effectiveDate)
    if (daycounter == 'Actual/365') {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            days = 366;
        } else {
            days = 365;
        }
    } else {
        days = Boolean(daycounter)? daycounter.replace(/[^\d]/g, ''): 365;
    }
    return _.divide(term, days);
}

/**
 * 对日期做加减
 * @param dataStr 例如 2022-12-19
 * @param dateOff
 */
export function addDate(dataStr,dateOff) {
    let a = moment(dataStr)
    let b=a.add(dateOff,"days");
    return b.format('YYYY-MM-DD');
}

/**
 *  str为该列的字段名(传字符串);
 *  tableData为该表格的数据源(传变量);
 *  minWidth为最小宽度，默认返回值
 **/
export function flexColumnWidth(str, tableData,minWidth) {
    if(!isNotVoid(minWidth)){
        minWidth=100;
    }
    //无数据时为自动宽度
    if(tableData==undefined){return minWidth+'px';}
    str = str + ''
    let columnContent = ''
    if (!tableData || !tableData.length || tableData.length === 0 || tableData === undefined) {
        return minWidth+'px';
    }
    if (!str || !str.length || str.length === 0 || str === undefined) {
        return minWidth+'px';
    }
    // 获取该列中最长的数据(内容)
    let index = 0
    for (let i = 0; i < tableData.length; i++) {
        if (tableData[i][str] === null) {
            return minWidth+'px';
        }
        const now_temp = tableData[i][str] + ''
        const max_temp = tableData[index][str] + ''
        if (now_temp.length > max_temp.length) {
            index = i
        }
    }
    columnContent = tableData[index][str]
    if(columnContent==undefined){
        return minWidth+'px';
    }
    // 以下分配的单位长度可根据实际需求进行调整
    let flexWidth = 0
    for (let i=0; i < columnContent.length; i++) {
        const char=columnContent[i];
        if ((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z')) {
            // 如果是英文字符，为字符分配8个单位宽度
            flexWidth += 8
        } else if (char >= '\u4e00' && char <= '\u9fa5') {
            // 如果是中文字符，为字符分配15个单位宽度
            flexWidth += 20
        } else {
            // 其他种类字符，为字符分配8个单位宽度
            flexWidth += 9
        }
    }
    if (flexWidth < 80) {
        // 设置最小宽度
        flexWidth = 80
    }
    // 设置最小宽度为配置宽度
    if(flexWidth<minWidth){
        flexWidth=minWidth;
    }
    return flexWidth + 'px'
}
/*
*
* 前端分页工具类
* 使用时需要如下方式
*
*
<el-table :data="FrontPageUtil.getTableData(LogDetailList)">
</el-table>
<el-pagination
    background align="right"
    layout=" -> ,prev, pager, next ,total,sizes"
    :total="FrontPageUtil.state.total"
    @current-change="(e)=>{FrontPageUtil.handleCurrentChange(e)}"
    @size-change="(e)=>{FrontPageUtil.handleSizeChange(e)}"
/>
*
* const FrontPageUtil = new FrontPageClass()
* */
export class FrontPageClass {
    constructor() {
        this.state = reactive({
            page: 1,
            limit: 10,
            total: 10,
        })
    }

    getTableData(e) {
        let state = this.state
        state.total = e.length
        return e.filter(
            (item, index) =>
                index < state.page * state.limit &&
                index >= state.limit * (state.page - 1)
        );
    }
    handleCurrentChange (e) {
        this.state.page = e;
    }
    handleSizeChange(e){
        this.state.limit = e;
    }

}